package org.tis.tools.abf.module.sys.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.sys.controller.request.SysSeqnoResetRequest;
import org.tis.tools.abf.module.sys.entity.SysSeqno;
import org.tis.tools.abf.module.sys.service.ISysSeqnoService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;

/**
 * sysSeqno的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/sysSeqno")
@Validated
@Slf4j
public class SysSeqnoController extends BaseController<SysSeqno>  {

    @Autowired
    private ISysSeqnoService sysSeqnoService;

    @ApiOperation(value = "新增序号资源", notes = "传入SysSeqnoResetRequest类作为参数")
    @OperateLog(type = OperateType.ADD, desc = "新增序号资源")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "SysSeqnoResetRequest",name = "传入的SysSeqnoResetRequest类", required = true) SysSeqnoResetRequest sysSeqnoResetRequest) {
        sysSeqnoService.add(sysSeqnoResetRequest);
        return ResultVO.success("添加成功");
    }

    @ApiOperation(value = "修改序号资源", notes = "传入SysSeqnoResetRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改序号资源")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "SysSeqnoResetRequest",name = "传入的SysSeqnoResetRequest类", required = true) SysSeqnoResetRequest sysSeqnoResetRequest) {
        SysSeqno sysSeqno = sysSeqnoService.selectById(sysSeqnoResetRequest.getSeqKey());
        if (sysSeqno == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        SysSeqno sysSeqnoChange = sysSeqnoService.change(sysSeqnoResetRequest);
        return ResultVO.success("修改成功！",sysSeqnoChange);
    }

    @ApiOperation(value = "根据Key删除序号资源", notes = "传入序号键值(seqKey)作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除序号资源")
    @DeleteMapping("/{seqKey}")
    public ResultVO delete(@PathVariable @NotBlank(message = "seqKey不能为空") @ApiParam(value = "seqKey",name = "序号键值", required = true) String seqKey) {
        SysSeqno sysSeqno = sysSeqnoService.selectById(seqKey);
        if (sysSeqno == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        sysSeqnoService.deleteById(seqKey);
        return ResultVO.success("删除成功");
    }

    @ApiOperation(value = "根据Key查询序号资源", notes = "传入序号键值(seqKey)作为参数")
    @GetMapping("/{seqKey}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "seqKey",name = "序号键值", required = true) String seqKey) {
        SysSeqno sysSeqno = sysSeqnoService.selectById(seqKey);
        if (sysSeqno == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", sysSeqno);
    }


    @ApiOperation(value = "分页查询序号列表", notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<SysSeqno> page) {
        return  ResultVO.success("查询成功", sysSeqnoService.listFilter(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "重置序号", notes = "传入序号列表的GUID作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "重置序号")
    @PutMapping("/resetSeq/{id}")
    public ResultVO resetSeq(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "序号列表的GUID", required = true) String id ){
        SysSeqno sysSeqno = sysSeqnoService.selectById(id);
        if (sysSeqno == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        sysSeqno.setSeqNo(BigDecimal.valueOf(0));
        sysSeqnoService.updateById(sysSeqno);
        return ResultVO.success("重置成功",sysSeqno);
    }
}

